package com.mc.thread;

public class ThreadVariable{
	private ThreadLocal<Integer> tl;
	
	public ThreadVariable(){
		tl = new ThreadLocal<Integer>(){
			public Integer initialValue(){
				return 0;
			}
		};
	}
	public void testThreadLocal(){
		tl.set(tl.get()+1);
		System.out.println(Thread.currentThread().getName()+":"+tl.get());
	}
	
	private static class TestThread implements Runnable{
		ThreadVariable tv;
		public TestThread(ThreadVariable tv){
			this.tv = tv;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			for(int i=0;i<4;i++)
				tv.testThreadLocal();
		}
	}
	
	public static void main(String[] args) {
		ThreadVariable tv = new ThreadVariable();
		for(int i=0;i<5;i++){
			Thread p = new Thread(new TestThread(tv));
			p.start();
		}
		
	}
}
